<template>
  <div class="addAcc">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>添加账号</span>
      </div>
      <div>
        <el-form
          :model="loginBtn"
          :rules="rules"
          ref="loginBtn"
          label-width="100px"
        >
          <el-form-item label="账号" prop="account">
            <el-input
              type="text"
              v-model="loginBtn.account"
              autocomplete="off"
              placeholder="请输入账号"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              type="password"
              v-model="loginBtn.password"
              autocomplete="off"
              placeholder="请输入密码"
              show-password
            ></el-input>
          </el-form-item>

          <!-- 用户组 -->

          <el-form-item label="用户组" prop="">
            <el-select v-model="loginBtn.userGroup" placeholder="请选择用户组">
              <el-option value="普通管理员">普通管理员 </el-option>
              <el-option value="超级管理员">超级管理员 </el-option>
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm">确定</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import { userAdd } from "../../api/user";
export default {
  data() {
    return {
      loginBtn: {
        account: "",
        password: "",
        userGroup: "",
      },
      rules: {
        //账号
        username: [
          { required: true, message: "请输入账号", trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: "长度在 2 到 10 个字符",
            trigger: "blur",
          },
        ],
        //密码
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 10,
            message: "长度在 6 到 10 个字符",
            trigger: "blur",
          },
        ],

        //分组验证
        userGroup: [
          { required: true, message: "请选择用户组", trigger: "blur" },
        ],
      },
    };
  },

  methods: {
    submitForm() {
      this.$refs.loginBtn.validate(async (item) => {
        //console.log(this.$refs.loginBtn);
        if (item) {
          let res = await userAdd(this.loginBtn);
          console.log(res);
          let { code, msg } = res;
          if (code === 0) {
            this.$message({
              message: msg,
              type: "success",
            });
            //添加账号成功后，跳转到账号列表
            this.$router.push('/acc')
          } else {
             this.$message.error(msg);
          }
        } else {
          console.log("添加失败");
        }
      });
    },
    //重置
    resetForm() {
      this.$refs.loginBtn.resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
.addAcc {
  width: 600px;

  .el-input {
    width: 70% !important;
  }
  .el-select {
    width: 30% !important;
  }
}
</style>